package com.ykh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ykh.mapper.FocusedMapper;
import com.ykh.mapper.UserMapper;
import com.ykh.pojo.po.Focused;
import com.ykh.pojo.po.User;
import com.ykh.pojo.vo.FocusUserVo;
import com.ykh.pojo.vo.Result;
import com.ykh.pojo.vo.UserVo;
import com.ykh.service.FocusedService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2023-06-08
 * Time: 13:59
 */
@Service
public class FocusedServiceImpl implements FocusedService {

    @Autowired
    FocusedMapper focusedMapper;

    @Autowired
    UserMapper userMapper;
    /**
     * 关注用户
     * @param account 被关注用户
     * @param user 关注发起方account
     * @return
     */
    @Override
    @Transactional
    public Result focusUser(String account, User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, account);
        User sysUser1 = userMapper.selectOne(queryWrapper);
        if(sysUser1 == null){
            return Result.fail(405,"关注的用户不存在");
        }
        //判断是否已关注过该用户
        queryWrapper1.eq(Focused::getLaunchId, user.getAccount());
        queryWrapper1.eq(Focused::getFocusedId, account);
        queryWrapper1.last("limit 1");
        Focused focused1 = focusedMapper.selectOne(queryWrapper1);
        if(focused1 != null){
            return Result.fail(405,"已关注过该用户");
        }
        Focused focused = new Focused();
        focused.setLaunchId(user.getAccount());
        focused.setFocusedId(sysUser1.getAccount());
        focusedMapper.insert(focused);

        queryWrapper2.eq(User::getAccount, user.getAccount());
        sysUser1.setFansNum(sysUser1.getFansNum()+1);
        user.setFocusNum(user.getFocusNum() + 1);
        //增加粉丝数,更新关注数
        userMapper.update(sysUser1,queryWrapper);
        userMapper.update(user,queryWrapper2);
        return Result.success("关注成功");
    }

    /**
     * 取消关注用户
     * @param
     * @param
     * @return
     */
    @Override
    @Transactional
    public Result cancelFocus(String account, User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();

        queryWrapper.eq(User::getAccount, account);
        User sysUser1 = userMapper.selectOne(queryWrapper);


        if(sysUser1 == null){
            return Result.fail(405,"关注的用户不存在");
        }
        queryWrapper1.eq(Focused::getLaunchId, user.getAccount());
        queryWrapper1.eq(Focused::getFocusedId, account);
        Focused focused = focusedMapper.selectOne(queryWrapper1);
        if(focused == null){
            return Result.fail(405,"未关注该用户");
        }
        queryWrapper2.eq(User::getAccount,  user.getAccount());
        User sysUser2 = userMapper.selectOne(queryWrapper2);
        focusedMapper.delete(queryWrapper1);
        sysUser1.setFansNum(sysUser1.getFansNum()-1);
        sysUser2.setFocusNum(sysUser2.getFocusNum()-1);
        userMapper.update(sysUser1,queryWrapper);
        userMapper.update(sysUser2,queryWrapper2);
        return Result.success("取消关注成功");
    }

    /**
     * 获取关注用户的列表
     * @return
     */
    @Override
    public Result getMyFocusList(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        //获取关注用户的id列表
        queryWrapper1.eq(Focused::getLaunchId, user.getAccount());
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper1);
        if(focuseds.size() <= 0){
            return Result.fail(405,"未关注用户");
        }

        List<String> focusedUserIdList = new ArrayList<>();
        for (Focused focused: focuseds) {
            focusedUserIdList.add(focused.getFocusedId());
        }
        //查询用户表,获取用户信息
        queryWrapper.in(User::getAccount,focusedUserIdList);
        List<User> focusedUserList = userMapper.selectList(queryWrapper);
        List<FocusUserVo> userVoList = copyList(focusedUserList);
        return Result.success(userVoList);
    }


    /**
     * 获取关注自己的用户信息列表
     * @return
     */
    @Override
    public Result getFocusedList(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        //获取关注用户的id列表
        queryWrapper1.eq(Focused::getFocusedId, user.getAccount());
        List<Focused> focuseds = focusedMapper.selectList(queryWrapper1);
        if(focuseds.size() <= 0){
            return Result.success("未被用户关注");
        }
        List<String> launchUserIdList = new ArrayList<>();
        for (Focused focused: focuseds) {
            launchUserIdList.add(focused.getLaunchId());
        }

        //查询用户表,获取用户信息
        queryWrapper.in(User::getAccount, launchUserIdList);
        List<User> launchUserList = userMapper.selectList(queryWrapper);
        List<FocusUserVo> focusUserVoList = copyList(launchUserList);
        return Result.success(focusUserVoList);
    }

    /**
     * 判断是否关注过该用户
     * @param account
     * @param user
     * @return
     */
    @Override
    public Result isFocus(String account, User user) {
        //判断是否关注过该用户
        LambdaQueryWrapper<Focused> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Focused::getLaunchId, user.getAccount());
        queryWrapper1.eq(Focused::getFocusedId, account);
        queryWrapper1.last("limit 1");
        Focused focused1 = focusedMapper.selectOne(queryWrapper1);
        if(focused1 == null){
            return Result.success(0);
        }
        return Result.success(1);
    }

    private List<FocusUserVo> copyList(List<User> focusedUserList) {
        List<FocusUserVo> focusUserVoList = new ArrayList<>();
        for (User user: focusedUserList) {
            focusUserVoList.add( copy(user));
        }
        return focusUserVoList;
    }

    private FocusUserVo copy(User user) {
        FocusUserVo focusUserVo = new FocusUserVo();
        BeanUtils.copyProperties(user,focusUserVo);
        return focusUserVo;
    }

}
